import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-03-30
 * Time: 12:21
 */
public class myStack<T> {
    public Object[] elem;
    private int curSize;

    public myStack() {
        this.elem = new Object[10];
    }

    public T push(T val) {
        this.elem[curSize] = val;
        curSize++;
        return val;
    }

    public T pop() {
        checkCapacity();
        Object ret = elem[curSize - 1];
        curSize--;
        return (T) ret;
    }

    public T peek() {

        return (T) elem[curSize - 1];
    }

    public boolean empty() {
        return curSize == 0;
    }

    public int search(T val) {
        for (int i = 0; i < curSize; i++) {
            if (elem[i].equals(val)) {
                return i;
            }
        }
        return -1;
    }

    public int size() {

        return this.elem.length;

    }

    private void checkCapacity() {
        if (curSize == elem.length) {
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        str.append("[");
        for (int i = 0; i < curSize; i++) {
            str.append(elem[i]);
            if (i != curSize - 1) {
                str.append(", ");
            }
        }
        str.append("]");
        return str.toString();
    }
}
